package com.qiao.glod.ui.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.qiao.glod.R;
import com.qiao.glod.api.GoldApi;
import com.qiao.glod.api.ICallback;
import com.qiao.glod.bean.GiftBean;
import com.qiao.glod.util.NetUtils;

import org.byteam.superadapter.SuperAdapter;
import org.byteam.superadapter.internal.SuperViewHolder;

import java.util.List;

/**
 * Created by Qiao on 2016/4/7.
 * function：
 */
public class ContentFragment extends QFragment implements SwipeRefreshLayout.OnRefreshListener{
    SwipeRefreshLayout swipeRefreshLayout;
    SuperAdapter<GiftBean> mAdapter;
    String type ;
    int page=1;

    @Override
    protected int generateResId() {
        return R.layout.fragment_main;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        type = getArguments().getString("type");
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        swipeRefreshLayout = findView(R.id.swipe);
        swipeRefreshLayout.setOnRefreshListener(this);

        bindRecyclerView();

    }

    private void bindRecyclerView(){
        RecyclerView recyclerView = findView(R.id.recyclerview);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.setHasFixedSize(true);

        recyclerView.setAdapter(mAdapter = new SuperAdapter<GiftBean>(getContext(),
                loadData(),
                R.layout.gifts_item_layout){
            @Override
            public void onBind(SuperViewHolder holder, int viewType, int position, GiftBean item) {
                holder.setText(R.id.txt_gifts_title,item.getDesc());
                holder.setText(R.id.gifts_author,item.getWho());
                Glide.with(getContext())
                        .load(item.getImg())
                        .into((ImageView) holder.getView(R.id.img_gifts_img));
            }
        });
    }

    private List<GiftBean> loadData(){
        List<GiftBean> data = GiftBean.getGitfs(type,GoldApi.LOAD_LIMIT,page);
        if(data==null || data.size()==0){
            //判断有没有网络
            if (NetUtils.isNetWorkConection(context)) {
                //进来就自动刷新
                swipeRefreshLayout.post(new Runnable() {
                    @Override
                    public void run() {
                        swipeRefreshLayout.setRefreshing(true);
                        onRefresh();
                    }
                });
            }
        }
        return data;
    }

    @Override
    public void onRefresh() {
       GiftBean.loadOnLine(type, GoldApi.LOAD_LIMIT, page, new ICallback<List<GiftBean>>() {
           @Override
           public void onSuccess(List<GiftBean> data) {
               swipeRefreshLayout.setRefreshing(false);

               data = GiftBean.getGitfs(type,GoldApi.LOAD_LIMIT, page);

               if(page==1){
                   mAdapter.replaceAll(data);
               }else {
                   mAdapter.addAll(data);
               }
           }

           @Override
           public void onFailed(String message) {
               swipeRefreshLayout.setRefreshing(false);
           }
       });
    }
}
